package com.example.hm_dtfkz.util;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.annotation.NonNull;

import java.util.List;

public class ShareUtil {
    private static final int TYPE_QQ = 0;
    private static final int TYPE_WECHAT = 1;

    private static final String PACKAGE_WECHAT = "com.tencent.mm";
    private static final String CLS_WECHAT = "com.tencent.mm.ui.tools.ShareImgUI";

    private static final String PACKAGE_MOBILE_QQ = "com.tencent.mobileqq";
    private static final String CLS_MOBILE_QQ = "com.tencent.mobileqq.activity.JumpActivity";

    // 判断是否安装指定app
    private static boolean isInstallApp(Context context, String appPackage) {
        final PackageManager pm = context.getPackageManager();
        List<PackageInfo> pInfo = pm.getInstalledPackages(0);
        for (PackageInfo packageInfo : pInfo) {
            ApplicationInfo applicationInfo = packageInfo.applicationInfo;
            // 非系统应用
            if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                String packageName = packageInfo.packageName;
//                Log.e("ShareUtil", packageName);
                if (appPackage.equals(packageName)) {
                    return true;
                }
            }
        }
        return false;
    }

    @NonNull
    private static Intent getIntent(int type) {
        String pkg = PACKAGE_MOBILE_QQ;
        String cls = CLS_MOBILE_QQ;
        if (TYPE_WECHAT == type) {
            pkg = PACKAGE_WECHAT;
            cls = CLS_WECHAT;
        }

        Intent intent = new Intent(Intent.ACTION_SEND);
        // 遍历所有支持发送图片的应用。找到需要的应用
        ComponentName cop = new ComponentName(pkg, cls);
        intent.setComponent(cop);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return intent;
    }

    /**
     * 直接分享纯文本内容至QQ好友
     *
     * @param context 上下文对象
     * @param content 要分享的内容
     */
    public static void shareTextToQQ(Context context, String content) {
        if (isInstallApp(context, PACKAGE_MOBILE_QQ)) {
            Intent intent = getIntent(TYPE_QQ);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, content);
            context.startActivity(intent);
        } else {
            Toast.makeText(context, "您需要安装QQ客户端", Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 分享图片给QQ好友
     *
     * @param context 上下文对象
     * @param bitmap  要分享的位图
     */
    public static void shareImageToQQ(Context context, Bitmap bitmap) {
        if (isInstallApp(context, PACKAGE_MOBILE_QQ)) {
            try {
                Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(
                        context.getContentResolver(), bitmap, "IMG_" + System.currentTimeMillis(), null));
                Intent intent = getIntent(TYPE_QQ);
                intent.setType("image/*");
                intent.putExtra(Intent.EXTRA_STREAM, uri);

                context.startActivity(Intent.createChooser(intent, "分享到"));
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(context, "分享失败：" + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(context, "您需要安装QQ客户端", Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 直接分享文本到微信好友
     *
     * @param context 上下文
     * @param content 要分享的内容
     */
    public static void shareTextToWechat(Context context, String content) {
        if (isInstallApp(context, PACKAGE_WECHAT)) {
            Intent intent = getIntent(TYPE_WECHAT);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, content);
            intent.putExtra("Kdescription", !TextUtils.isEmpty(content) ? content : "");

            context.startActivity(intent);
        } else {
            Toast.makeText(context, "您需要安装微信客户端", Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 直接分享图片到微信好友
     *
     * @param context 上下文对象
     * @param uri     要分享的图片Uri
     */
    public static void shareImageToWechat(Context context, Uri uri) {
        if (isInstallApp(context, PACKAGE_WECHAT)) {
            Intent intent = getIntent(TYPE_WECHAT);
            intent.setType("image/*");
            intent.putExtra(Intent.EXTRA_STREAM, uri);

            context.startActivity(Intent.createChooser(intent, "分享到"));
        } else {
            Toast.makeText(context, "您需要安装微信客户端", Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 直接分享图片到微信好友
     *
     * @param context 上下文对象
     * @param bitmap  要分享的位图
     */
    public static void shareImageToWechat(Context context, Bitmap bitmap) {
        if (isInstallApp(context, PACKAGE_WECHAT)) {
            try {
                Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(
                        context.getContentResolver(), bitmap, "IMG_" + System.currentTimeMillis(), null));
                Intent intent = getIntent(TYPE_WECHAT);
                intent.setType("image/*");
                intent.putExtra(Intent.EXTRA_STREAM, uri);

                context.startActivity(Intent.createChooser(intent, "分享到"));
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(context, "分享失败：" + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(context, "您需要安装微信客户端", Toast.LENGTH_LONG).show();
        }
    }
}
